package com.neulogistics.core.customercenter.domain.model.aggregate;

import com.neulogistics.core.customercenter.domain.model.enumeration.OrderStatus;
import com.neulogistics.core.customercenter.domain.model.enumeration.OrderType;
import com.neulogistics.core.customercenter.domain.model.valueobject.Address;
import com.neulogistics.core.customercenter.domain.model.valueobject.OrderItem;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Entity;
import com.neulogistics.infrastructure.ddd.domain.seedwork.IAggregateRoot;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;
import java.util.List;

@Data
public class Order extends Entity<Long> implements IAggregateRoot {
    /**
     * 订单id
     */
    private Long orderId;

    /**
     * 订单类型：1：配送单；2：退货单；3：换货单
     */
    private OrderType orderType;

    /**
     * 客户id
     */
    private Integer customerId;

    /**
     * 订单项，用来储存订单中的商品信息，包含商品id、数量、单价、计量单位
     */
    private List<OrderItem> orderItems;

    /**
     * 订单状态：0:新建；1:缺货；2:待调度；3:待出库；4:运输中；5:到达分站；6:派送中；7:已完成；8:已退货；9:已换货
     */
    private OrderStatus orderStatus;

    /**
     * 总额
     */
    private Double totalAmount;

    /**
     * 备注信息
     */
    private String orderRemark;

    /**
     * 收货地址{省，市，区，详细}
     */
    private Address receiverAddress;

    /**
     * 收货人名
     */
    private String receiverName;

    /**
     * 收货人电话
     */
    private String receiverPhone;

    /**
     * 是否需要发票：0:不需要；1:需要
     */
    private Integer optInvoice;

    /**
     * 发票id
     */
    private Long invoiceId;

    /**
     * 期望配送时间
     */
    private Date expDeliveryTime;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 创建人id
     */
    private Integer creatorId;

    /**
     * 发货时间
     */
    private Date shippedTime;

    /**
     * 成交时间
     */
    private Date completeTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否删除
     */
    private Integer deleted;

    public Order(){}

    public Order(Long orderId, OrderType orderType, Integer customerId,
                 List<OrderItem> orderItems, String orderRemark,
                 Address receiverAddress, String receiverName,
                 String receiverPhone, Integer optInvoice,
                 Date expDeliveryTime) {
        this.orderId = orderId;
        this.orderType = orderType;
        this.customerId = customerId;
        this.orderItems = orderItems;
        this.orderStatus = OrderStatus.Created;
        this.orderRemark = orderRemark;
        this.receiverAddress = receiverAddress;
        this.receiverName = receiverName;
        this.receiverPhone = receiverPhone;
        this.optInvoice = optInvoice;
        this.expDeliveryTime = expDeliveryTime;
        this.totalAmount = calculateTotalAmount();
    }

    public double calculateTotalAmount(){
        int sum = 0;
        for (OrderItem item : orderItems){
            sum += item.getGoodsPrice() * item.getCount();
        }
        return sum;
    }


}
